package com.buka.communicate.appliction;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.buka.communicate.utils.SpUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.buka.communicate.entity.User;
import com.buka.communicate.utils.Tools;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.squareup.leakcanary.LeakCanary;

import net.neiquan.applibrary.base.BaseActivity;
import net.neiquan.applibrary.numberpicker.view.AlertDatePicker;

import org.haitao.common.user.UserUtils;
import org.haitao.common.utils.FileUtils;
import org.haitao.common.utils.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;


/**
 * 作者 ： dyj
 * 时间 ： 2016/1/29.
 */

public class MyApplication extends Application  {

    public static MyApplication instance;
    public static Context context;
    /**
     * Activity集合
     */
    private static ArrayList<BaseActivity> activitys = new ArrayList<BaseActivity>();
    //  private OkHttpFinalConfiguration.Builder builder;
    public User user;

    @Override
    public void onCreate() {
        MultiDex.install(this);
        super.onCreate();
        FileUtils.init(this);
        instance = this;
        Fresco.initialize(this);// 耗时300 毫秒
        context = this;
        initUM();
        //设置新浪微博的回调地址
//        Config.REDIRECT_URL = "http://sns.whalecloud.com/sina2/callback";
        initPush();
        user = SpUtil.getBeanByFastJson(this, "user", User.class);
        ToastUtil.init(instance);
        Tools.setContext(instance);
        initImageLoader(this);
        LeakCanary.install(this);
    }
    /**
     * 保存用户
     *
     * @param user
     */
    public void saveUser(User user) {
        this.user = user;
        SpUtil.saveBeanByFastJson(this, "user", user);
    }

    public boolean isLogin() {
        return user == null ? false : true;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void initPush() {
//        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
//        JPushInterface.init(this);            // 初始化 JPush
    }

    private void initUM() {
        //各个平台的配置，建议放在全局Application或者程序入口
        //微信 wx12342956d1cab4f9,a5ae111de7d9ea137e88a5e02c07c94d
//        PlatformConfig.setWeixin("", "");
        //豆瓣RENREN平台目前只能在服务器端配置
        //新浪微博
//        PlatformConfig.setSinaWeibo("", "");
//        PlatformConfig.setQQZone("", "");
    }

    /**
     * 添加Activity到ArrayList<Activity>管理集合
     *
     * @param activity
     */
    public void addActivity(BaseActivity activity) {
        String className = activity.getClass().getName();
        for (Activity at : activitys) {
            if (className.equals(at.getClass().getName())) {
                activitys.remove(at);
                break;
            }
        }
        activitys.add(activity);
    }
    public static void initImageLoader(Context context) {
        // Create default options which will be used for every
        // displayImage(...) call if no options will be passed to this method
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
        File file = FileUtils.getImageFile();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPoolSize(3) // default
                .threadPriority(Thread.NORM_PRIORITY - 1) // default
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .memoryCache(new UsingFreqLimitedMemoryCache((int) (Runtime.getRuntime().maxMemory() / 8)))
                .memoryCacheSizePercentage(13) // default
                .defaultDisplayImageOptions(defaultOptions)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs() // Remove for release app 日志log
                .diskCache(new UnlimitedDiskCache(file)).build();
        ImageLoader.getInstance().init(config);
    }
    public static MyApplication getInstance() {
        return instance;
    }

}
